#pragma once
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
#include "rapidxml_utils.hpp"
#include "rapidxml_iterators.hpp"
typedef rapidxml::xml_document<TCHAR> xmldoc;
typedef rapidxml::xml_attribute<TCHAR> xmlattri;



class xmlNode
{
public:
	typedef rapidxml::xml_node<TCHAR> node_type;
	xmlNode(){ mpNode = NULL; }
	xmlNode(node_type* pNode):mpNode(pNode){}

	xmlNode& operator=(node_type* pNode ){ mpNode = pNode; }
	operator bool()const{ return mpNode!=NULL; }

	xmlNode firstChild(TCHAR* sName=NULL)const{ return mpNode->first_node(sName); }
	xmlNode nextSibling(TCHAR* sName=NULL)const{ return mpNode->next_sibling(sName); }

	xmlNode& addAttribute(TCHAR*sAttri, const CString& val)
	{
		xmldoc* pdoc = mpNode->document();
		TCHAR* sval = pdoc->allocate_string(val.GetString(),val.GetLength()+1);
		mpNode->append_attribute( pdoc->allocate_attribute(sAttri,sval) );
		return *this;
	}

	xmlNode& addAttribute( TCHAR* sAttri, const CTime& val )
	{	
		CString sval= GtdTime::toString(val);	
		return addAttribute(sAttri,sval);		
	}

	bool getAttribute( TCHAR* sAttri, CString& val )
	{
		if(xmlattri* pattri = mpNode->first_attribute(sAttri))
		{
			val = pattri->value();
			return true;
		}
		return false;
	}

	bool getAttribute( TCHAR* sAttri, CGuid& val )
	{
		CString s;
		if(getAttribute(sAttri,s))
		{
			if(CGuid::Convert(s,val))
			{
				return true;
			}
		}
		return false;
	}

	bool getAttribute(TCHAR* sAttri, CTime& val )
	{
		CString s;
		if(getAttribute(sAttri,s))
		{
			if(GtdTime::formString(s,val))
			{
				return true;
			}
		}
		return false;
	}


	xmlNode createChild(TCHAR* name)
	{
		node_type* pchild = mpNode->document()->allocate_node(rapidxml::node_element,name);
		mpNode->append_node(pchild);
		return pchild;
	}
public:
	node_type* mpNode;
};
